package com.nattakute.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.hibernate.criterion.Subqueries;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Repository;

import com.nattakute.dao.UserDAO;
import com.nattakute.model.Role;
import com.nattakute.model.Users;

@Repository
public class UserDAOImpl implements UserDAO{

	@Autowired
	private SessionFactory sessionFactory;
	
	
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}


	@SuppressWarnings("unchecked")
	@Cacheable("users")
	public List<Users> getAll() {
		
		return sessionFactory.getCurrentSession().createQuery("from Users").list();
	}

	
	public Boolean insert(Users user) {
		
		try {
			sessionFactory.getCurrentSession().save(user);
			return true;
		} catch (Exception e) {
			
			return false;
		}
	}

	
	public Boolean update(Users user) {
		
		try {
			sessionFactory.getCurrentSession().update(user);
			return true;
		} catch (Exception e) {
			
			return false;
		}
	}

	
	public Boolean delete(Users user) {
		
		try {
			sessionFactory.getCurrentSession().delete(user);
			return true;
		} catch (Exception e) {
			
			return false;
		}
	}

	
	public Users findByUserId(Integer userId) {
		
		Users user = new Users();
		try {
			System.out.println("truoc khi tim");
			user =(Users)sessionFactory.getCurrentSession().get(Users.class, userId);
			System.out.println("da tim");
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println("loser");
		}
		return user;
	}

	
	public Boolean deleteUserById(Integer id) {
		
		try {
			sessionFactory.getCurrentSession().createQuery("delete from Users where UserID= :id").setParameter("id", id).executeUpdate();
			System.out.println("deleteUserById");
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}

	@SuppressWarnings("unchecked")
	
	public Users userLogin(String userName, String password) {
		Criteria criteria=sessionFactory.getCurrentSession().createCriteria(Users.class);
		DetachedCriteria detachedCriteria=DetachedCriteria.forClass(Role.class);
		detachedCriteria.add(Restrictions.eq("roleID", 1)).setProjection(Projections.property("roleID"));
		
		@SuppressWarnings("unused")
		List<Users> users=new ArrayList<Users>();
		Users user=null;
		try {
			criteria.add(Subqueries.propertyNotIn("userID", detachedCriteria));
			criteria.add(Restrictions.eq("userName", userName));
			criteria.add(Restrictions.eq("password", password));
			criteria.add(Restrictions.eq("active",true));
			users=(List<Users>) criteria.list();
			user=(Users) criteria.list().get(0);
		} catch (Exception e) {
		}
		if(user!=null){
			return user;
		}
		else {
			return null;
		}
		/*Object ob=new Object();
		try{
			System.out.println(userName);
			System.out.println(password);
		 	ob=(Object) sessionFactory.getCurrentSession().createSQLQuery("select * from Users where userName= :userName and password = :password and userId not in ( select userId from Users where roleId = 1 )"
					)
					.setParameter("userName", userName)
					.setParameter("password", password).list().get(0);
			return true;
		}catch(Exception e){
			System.out.println("dang nhap that bai");
			e.printStackTrace();
			return false;
		}*/
	}

	
	public Users adminLogin(String userName, String passwod) {
		Users user=new Users();
		try{
			Criteria criteria = sessionFactory.getCurrentSession().createCriteria(Users.class);
			Criteria criteria2 = criteria.createCriteria("roleID");
			criteria.add(Restrictions.eq("userName", userName)).add(Restrictions.eq("password", passwod));
			criteria2.add(Restrictions.eq("roleID", 1));
			
			user = (Users)criteria2.list().get(0);
			
//			user=(Users)sessionFactory.getCurrentSession().createQuery("from Users where userName= :userName and password = :password and roleID= :roleID")
//					.setParameter("userName", userName)
//					.setParameter("password", passwod)
//					.setParameter("roleID", 1).list().get(0);
//			System.out.println("dang nhap admin thanh cong");
//			return user;
			return user;
		}catch(Exception e){
			System.out.println("dang nhap that bai");
			return null;
		}
	}

	@SuppressWarnings("unchecked")
	
	public List<Users> notActive() {
		
		 List<Users> users=sessionFactory.getCurrentSession().createQuery("from Users where active = 0").list();
		 return users;
	}

	
	public Boolean activeUser(Users user) {
		
		user.setActive(true);
		try {
			sessionFactory.getCurrentSession().update(user);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return true;
		}
		 
	}

	
	@SuppressWarnings("unchecked")
	public List<Users> getUserNotAdmin() {
		List<Users> users = new ArrayList<Users>();
		Criteria criteria = sessionFactory.getCurrentSession().createCriteria(Users.class);
		Criteria criteriaRole = criteria.createCriteria("roleID");
		criteriaRole.add(Restrictions.not(Restrictions.eq("roleID", 1)));
		try {
			users = criteriaRole.list();
			if(users != null){
				return users;
			}
		} catch (Exception e) {
			return null;
		}
		return null;
	}

	@SuppressWarnings("unchecked")
	
	public Users getUserByName(String userName) {
		List<Users> users = new ArrayList<Users>();
		users = sessionFactory.getCurrentSession().createQuery("from Users where userName =:userName").setString("userName", userName).list();
		
//		try {
//			System.out.println("truoc khi tim");
//			user =(Users)sessionFactory.getCurrentSession().load(Users.class, userName);
//			System.out.println("da tim");
//		} catch (Exception e) {
//			e.printStackTrace();
//			System.out.println("loser");
//		}
//		return user;
		
		return users.get(0);
	}

	
	@SuppressWarnings("unchecked")
	public List<Users> getInstructor() {
		List<Users> listUsers = new ArrayList<Users>();
		try {
			Criteria criteriaUser = sessionFactory.getCurrentSession().createCriteria(Users.class);
			Criteria criteriaRole = criteriaUser.createCriteria("roleID");
			criteriaRole.add(Restrictions.eq("roleID", 3));
			listUsers = criteriaRole.list();
			return listUsers;
		} catch (Exception e) {
			return null;
		}
	}
}
